Git Commands Cheat Sheet - WittyWriter

Git Commands Cheat Sheet

📘 Key Concepts and Definitions

🧮 Command Syntax Patterns

Git commands follow a standard structure, which helps in understanding how to use them.

git <command> [<subcommand>] [-options] [<arguments>]

🛠️ Tools, Commands, or Syntax

Configuration & Setup

Making Changes (Local)

Branching & Merging

Working with Remotes

🧭 Step-by-Step Guides or Workflows

Basic Feature Branch Workflow

  1. Update your local main branch:
    git checkout main
    git pull origin main
  2. Create a new branch for your feature:
    git checkout -b new-feature-branch
  3. Make your changes: Edit, create, and delete files.
  4. Stage and commit your changes:
    git add .
    git commit -m "Add new feature X"
  5. Push your branch to the remote repository:
    git push -u origin new-feature-branch
  6. Open a Pull Request (PR) on the repository's web interface (e.g., GitHub).
  7. Once the PR is approved and merged, you can safely delete your local branch:
    git checkout main
    git branch -d new-feature-branch
Best Practice: Always work on feature branches, not directly on the main branch. This keeps the main line of development clean and stable.

⌨️ Shortcuts and Productivity Tips

Git Aliases

Create shortcuts for long commands in your .gitconfig file.

Time-Saving Tricks

📊 Tables, Charts, or Visual Aids

Flow of a File in Git


 Working Directory     --->    Staging Area     --->    Local Repository
 (Untracked files)     |         (Index)          |       (.git directory)
                       |                          |
   (modified file) --git add--> (staged file) --git commit--> (versioned file)
        

Merge vs. Rebase

Aspect git merge git rebase
History Preserves history as it happened. Creates a "merge commit". Rewrites history to create a linear, cleaner timeline.
Collaboration Safer for public/shared branches as it doesn't change existing commits. Can be dangerous on shared branches. Best used on private feature branches.
Complexity Simpler to use and understand. More powerful, but can be more complex to resolve conflicts.

🧪 Examples and Use Cases

Problem: I committed something, but forgot to add a file.

You don't need a new commit. You can amend the previous one.

  1. Add the file you forgot: git add [forgotten-file]
  2. Amend the previous commit: git commit --amend --no-edit
The --no-edit flag keeps the original commit message. Omit it to write a new one. Be careful amending commits that have already been pushed.

Problem: I need to sync my forked repository with the original.

  1. Add the original repository as a remote (you only do this once):
    git remote add upstream [original-repo-url]
  2. Fetch changes from the original repository:
    git fetch upstream
  3. Checkout your main branch and merge the changes:
    git checkout main
    git merge upstream/main

🧹 Troubleshooting and Debugging

Warning: Be very careful with commands that rewrite history (like git reset --hard and git rebase) on branches that are shared with other people.

📚 References and Further Reading

🍪 We use cookies to improve your experience. Learn more